package main

import (
	".."
	"fmt"
	"os"
	"os/signal"
	"syscall"
)

type Proto struct {
	buff []byte
}

func (p *Proto) Msg() []byte {
	return p.buff
}

type CallBack struct {
}

func (c CallBack) OnReadMsg(conn *network.Connection) (network.Message, error) {
	fmt.Println("OnReadMsg")
	proto := &Proto{
		buff: make([]byte, 1024),
	}
	_, err := conn.Conn.Read(proto.buff)
	if err != nil {
		return nil, err
	}
	return proto, nil
}

func (c CallBack) OnConnection(conn *network.Connection) {
	fmt.Println("OnConnection")
}
func (c CallBack) OnMessage(conn *network.Connection, msg network.Message) {
	fmt.Println("OnMessage")
	conn.SendPacket(msg)
}
func (c CallBack) OnClose(conn *network.Connection) {
	fmt.Println("OnClose")
}

func main() {
	tcp_server := network.NewTcpServer(CallBack{})
	tcp_server.Start("0.0.0.0", 8989)
	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	fmt.Println("signal: ", <-sig)
	tcp_server.Stop()
}
